package Control;

import Modele.Connect;
import Modele.Dep;
import Modele.Med;
import Modele.Pays;
import Modele.Spe;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Control extends HttpServlet {

    private Pays p;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    public void destroy() {
        Connect.close();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String choix = request.getParameter("choix");
        String undep = request.getParameter("dep");
        String nom = request.getParameter("nom");
        String spec = request.getParameter("spe");
        String page;
        switch (choix) {

            case "dep":
                if (undep == null) {
                    Collection<Dep> d = p.getLesDeps();
                    request.setAttribute("liste", d);
                    page = "departement.jsp";

                } else {

                    page = "medecin.jsp";

                    Dep ledep = p.getLeDep(undep);
                    Collection<Med> m = ledep.getLesMeds();
                    request.setAttribute("listeM", m);
                    request.setAttribute("d", ledep);

                }

                break;

            case "nom":
                if (nom == null) {
                    page = "nom.jsp";
                } else {
                    page = "listenom.jsp";

                    Collection<Med> lesMedecins = new TreeSet<Med>();
                    for (Dep unD : p.getLesDeps()) {
                        for (Med unM : unD.getLesMeds()) {
                            if (unM.getNom().startsWith(nom)) {
                                lesMedecins.add(unM);
                            }
                        }

                    }
                    request.setAttribute("listeMed", lesMedecins);
                    request.setAttribute("nom", nom);
                }
                break;

            case "spe":
                if (spec == null) {
                    Collection<Spe> s = p.getLesSpes();
                    request.setAttribute("listeS", s);
                    page = "spe.jsp";
                } else {
                    page = "listespe.jsp";
                    Spe laspe = p.getLaSpe(spec);
                    Collection<Med> m = laspe.getLesMeds();
                    request.setAttribute("listeMeds", m);
                    request.setAttribute("nomspe", spec);
                }
                break;
            default:
                page = "index.jsp";
                break;

        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
